from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtSql import *
import Ui_jurnalType
import jurnalTypeEdit
#import res

class JurnalType(QDialog, Ui_jurnalType.Ui_jurnalTypeDlg):
  def __init__(self, dbConn, dataGlob,  parent=None):
        super(JurnalType, self).__init__(parent)
        self.setupUi(self)       
        self.initMenu()
        self.db = dbConn
        self.dataGlob = dataGlob
        self.mastModel = QSqlQueryModel(self)
        self.mastModel.setHeaderData(0, Qt.Horizontal, QVariant("Prefix"))
        self.mastModel.setHeaderData(1, Qt.Horizontal, QVariant("Description"))
        
        self.master_tv.setModel(self.mastModel)
        self.master_tv.setSelectionMode(QTableView.SingleSelection)
        self.master_tv.setSelectionBehavior(QTableView.SelectRows)        
        
  @pyqtSignature("")
  def on_desc_le_editingFinished(self):
      if self.desc_le.text() != "":
          self.desc_cb.setCheckState(Qt.Checked)
      else:
          self.desc_cb.setCheckState(Qt.Unchecked)
                  
  def initMenu(self):
      self.retrieveAct = QAction(QIcon(":/retrieve.ico"), self.tr("Retrieve"), self)
#      self.retrieveAct.setShortcut ("Ctrl+R")
      self.connect(self.retrieveAct, SIGNAL("triggered()"), self.retrieveData)
      self.sortAct = QAction(QIcon(":/sort.ico"), self.tr("Sort"), self)
#      self.sortAct.setShortcut ("Ctrl+S")
      self.addAct = QAction(QIcon(":/add.ico"), self.tr("Add"), self)
#      self.addAct.setShortcut ("Ctrl+A")
      self.connect(self.addAct, SIGNAL("triggered()"), self.addData)
      self.editAct = QAction(QIcon(":/edit.ico"), self.tr("Edit"), self)
#      self.editAct.setShortcut ("Ctrl+E")
      self.connect(self.editAct, SIGNAL("triggered()"), self.editData)
      self.delAct = QAction(QIcon(":/del.ico"), self.tr("Delete"), self)
      self.connect(self.delAct, SIGNAL("triggered()"), self.delData)
#      self.delAct.setShortcut ("Ctrl+Del")
      self.printAct = QAction(QIcon("res/print.ico"), self.tr("Print Preview"), self)
#      self.printAct.setShortcut ("Ctrl+P")

  def retrieveData(self):
      qStr = "select prefix, desk from master_jurnal_type"
      filt = ""
      urut = " order by prefix "
      if self.desc_cb.checkState() == Qt.Checked:
          filt = filt + " desk like '"+self.desc_le.text()+"%' and "
          
      if filt != "":
          filt = " where "+filt.left(len(filt)-4)
          
      qStr = qStr + filt + urut
      self.mastModel.setQuery(qStr, self.db)
      self.master_tv.setColumnWidth(0, 100)
      self.master_tv.setColumnWidth(1, 300)
      self.master_tv.resizeRowsToContents()
      
  def addData(self):
      form = jurnalTypeEdit.JurnalTypeEdit(self.db, "add",  self.dataGlob, self)
      form.show()

  def editData(self):
      index = self.master_tv.currentIndex()
      if index.isValid():
          brs = self.mastModel.record(index.row())
          id = brs.value("prefix").toString()
          form = jurnalTypeEdit.JurnalTypeEdit(self.db, id,  self.dataGlob, self)
          form.show()

  def delData(self):
      index = self.master_tv.currentIndex()
      if index.isValid():
          brs = self.mastModel.record(index.row())
          id = brs.value("prefix").toString()
          nama = brs.value("desk").toString()
          if QMessageBox.question(self, "Journal Type","Delete Data : "+nama+"?", QMessageBox.Yes|QMessageBox.No) == QMessageBox.Yes:
              qMast = QSqlQuery(self.db)
              self.db.transaction()
              qMast.exec_("delete from master_jurnal_type where prefix = '"+id+"'")
              self.db.commit()
              self.retrieveData()

  def sortData(self):
       qStr = "select id_pelanggan, kode_pelanggan, nama, no_kartu, alamat1, kota1 from pelanggan"
       self.mastModel.setQuery(qStr, self.db)

  def printData(self):
       qStr = "select id_pelanggan, kode_pelanggan, nama, no_kartu, alamat1, kota1 from pelanggan"
       self.mastModel.setQuery(qStr, self.db)

  def contextMenuEvent(self, event):
        menu = QMenu(self)
        menu.addAction(self.retrieveAct)
        menu.addAction(self.sortAct)
        menu.addSeparator()
        menu.addAction(self.addAct)
        menu.addAction(self.editAct)
        menu.addSeparator()
        menu.addAction(self.delAct)
        menu.addSeparator()
        menu.addAction(self.printAct)
        menu.exec_(event.globalPos())

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    form = JurnalType()
    form.show()
    app.exec_()
